﻿using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNet.Identity;
using TeamPrasioliteWarGame.Models;

namespace TeamPrasioliteWarGame
{
    public partial class Units : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Entities context = new Entities();

            string userGuidId = User.Identity.GetUserId();

            var user = context.ExtendUsers.Include("AspNetUser").FirstOrDefault(u => u.UserId == userGuidId);
            var getBarrackInfo = user.AspNetUser.UserBuildings.FirstOrDefault(ub => ub.Building.Type == "Barracks");
            var recruitUnitsDataSource = new List<GameUnit>();

            //Check whether user have barracks or not
            if (getBarrackInfo != null)
            {
                recruitUnitsDataSource = context.GameUnits.ToList();
            }

            this.ListViewRecruitUnits.DataSource = recruitUnitsDataSource;
            this.ListViewRecruitUnits.DataBind();

            var ownUnitsDataSource = user.AspNetUser.UserUnits.ToList();

            this.ListViewUnits.DataSource = ownUnitsDataSource;
            this.ListViewUnits.DataBind();
        }
    }
}